package com.test.fundible.fundible_00_customer_service;

import static com.test.fundible.fundible_00_utils.FundibleUtil.startLoading;
import static com.test.fundible.fundible_00_utils.FundibleUtil.stopLoading;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Looper;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.test.fundible.R;
import com.test.fundible.fundible_00_utils.Loading_fundibleDialog;
import com.test.fundible.fundible_10_vip.Pers_fundibleonalIn_fundiblefo;
import com.test.fundible.fundible_00_http.OkHttpUtil;

public class Feed_fundiblebackActivity extends AppCompatActivity {

    private EditText descEt;
    private TextView countTv;

    private String type_fundible = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fundible_feedback);
        //tag2024
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);

        descEt = findViewById(R.id.afa_et_desc);
        countTv = findViewById(R.id.afa_tv_count);

        findViewById(R.id.afa_ib_back).setOnClickListener(v -> onBackPressed());
        findViewById(R.id.afa_ib_customerservice).setOnClickListener(v -> new Customer_fundibleServiceDialog(this).show());

        TextView[] typeTvs = new TextView[]{
            findViewById(R.id.afa_tv_type_payment),
            findViewById(R.id.afa_tv_type_app_bugs),
            findViewById(R.id.afa_tv_type_suggestion),
            findViewById(R.id.afa_tv_type_other)
        };

        for (int i=0;i<typeTvs.length;i++){
            final int fi = i;
            typeTvs[i].setOnClickListener(v -> {
                for (int j=0;j<typeTvs.length;j++){
                    if (fi==j){

                        type_fundible = typeTvs[j].getText().toString();
                        OkHttpUtil.getInstance_fundible().uploadEventPoint("FEEDBACK", type_fundible.toUpperCase()+"_CLICK","");

                        typeTvs[j].setBackgroundResource(R.drawable.bg_feedback_check);
                        typeTvs[j].setTextColor(getColor(R.color.white));

                    }else {
                        typeTvs[j].setBackgroundResource(R.drawable.bg_feedback_nocheck);
                        typeTvs[j].setTextColor(Color.parseColor("#FF828282"));
                    }
                }
            });
        }

        descEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
            @SuppressLint("SetTextI18n")
            @Override
            public void afterTextChanged(Editable s) {
                int length = s.length();
                countTv.setText(length+"/1000");
                if (length>=1000) countTv.setTextColor(Color.RED);
                else countTv.setTextColor(Color.parseColor("#FF9F9F9F"));
            }
        });

        descEt.setOnFocusChangeListener((v, hasFocus) ->
            OkHttpUtil.getInstance_fundible().uploadEventPoint("FEEDBACK","INPUT",hasFocus ? "":descEt.getText().toString().trim())
        );

        findViewById(R.id.afa_btn_submit).setOnClickListener(v -> {
            String feedback = descEt.getText().toString().trim();
            if (TextUtils.isEmpty(type_fundible) || TextUtils.isEmpty(feedback)) {
                Toast.makeText(this,getString(R.string.feedback_describe_title),Toast.LENGTH_SHORT).show();
                return;
            }
            startLoading(this,"");
            OkHttpUtil.getInstance_fundible().submitFb(type_fundible,feedback,this::onSubmitResponse_fundible);
        });

    }
    
    @Override
    protected void onResume() {
        super.onResume();
        OkHttpUtil.getInstance_fundible().uploadEventPoint("FEEDBACK","START","");
    }

    @Override
    protected void onPause() {
        super.onPause();
        OkHttpUtil.getInstance_fundible().uploadEventPoint("FEEDBACK","END","");
    }


    public void onSubmitResponse_fundible(){

        long currentTime = System.currentTimeMillis();
        long lastFeedbackTime = Pers_fundibleonalIn_fundiblefo.getInstance().getLastFeedbackTime();

        if (currentTime - lastFeedbackTime > 604800000L){//时隔一周提交反馈经验+3
            //经验+3
            Pers_fundibleonalIn_fundiblefo.getInstance().addExpValue(3);
            Pers_fundibleonalIn_fundiblefo.getInstance().setLastFeedbackTime(currentTime);
            Pers_fundibleonalIn_fundiblefo.getInstance().uploadUserInfo(this::onSubmitSuccess);
        }else onSubmitSuccess();

    }

    private void onSubmitSuccess(){

        runOnUiThread(() -> {

            stopLoading();

            Toast.makeText(this,R.string.tips_submit_success_fundible,Toast.LENGTH_SHORT).show();

        });

        new Thread(() -> {
            try {
                Thread.sleep(1001);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            runOnUiThread(this::onBackPressed);
        }).start();
    }

}